package com.amkai.aes.webservice.binary.objecthandler.registration;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.amkai.aes.aescommon.service.binary.job.registration.InsurancePolicyCaseVerifiationJob;
import com.amkai.aes.generatedbeans.insurance.InsurancePolicyCaseVerifiation;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class InsurancePolicyCaseVerifiationJobHandler
		implements
		JobObjectHandler<InsurancePolicyCaseVerifiationJob, Serializable, InsurancePolicyCaseVerifiation> {
	private static final Log logger = LogFactory.getLog(InsurancePolicyCaseVerifiation.class);
	
	private static String queryString = "select ipf from InsurancePolicyCaseVerifiation ipf left join fetch ipf.scdOpinionPhysician "+
			" left join fetch ipf.referalPhysician  left join fetch ipf.verifier " +
			" left join fetch ipf.pmCase as pmcase left join fetch pmcase.primaryPhysician left join fetch pmcase.patient as pat "+
			" left join fetch pat.person "+
			" where ipf.pmCase =  :case_id and ipf.insurancePolicy = :policy_id ";

	@Override
	public InsurancePolicyCaseVerifiation process(
			InsurancePolicyCaseVerifiationJob job, Serializable object)
			throws Exception {

		Query query = null;
		InsurancePolicyCaseVerifiation result = null;
		
		SessionManager sm = SiteManager.Start();
		try {
			
			if(( job.getCaseId() != null ) && (job.getInsurancePolicyId() != null ) ) {
				query = sm.execute(queryString).setLong("case_id", job.getCaseId()).setLong("policy_id", job.getInsurancePolicyId());
				result = (InsurancePolicyCaseVerifiation)query.uniqueResult();
			}
		} catch( Exception e ) {
			logger.error("InsurancePolicyCaseVerifiation: ", e);
			throw e;
		} finally {
			sm.closeSession();
		}
		return result;
	}
}
